/*
* @Author: ZP
* @Description: 请求封装
* @LastEditTime: 2024-03-08 15:05:04
*/
import axios from 'axios'
const httpRequestJpaasUrl = '/jpaas-jags-server/interface/'

const jpaasRequest = {
    // 获取签名
    createSign: (params = {}) => {
        return new Promise((resolved, rejected) => {
            axios({
                method: 'post',
                url: httpRequestJpaasUrl + 'createsign.do',
                data: params
            }).then((res) => {
                if (res.data.success) {
                    const { sign } = res.data.data
                    resolved(sign)
                } else {
                    rejected(res)
                }
            }).catch((error) => {
                rejected(error)
            })
        })
    },
    // 网关请求 
    gateway: (params = {}) => {
        return new Promise((resolved, rejected) => {
            axios({
                method: 'post',
                url: httpRequestJpaasUrl + 'gateway.do',
                data: params
            }).then((res) => {
                if (res.data.code == 200 && res.data.success == true) {
                    const objData = JSON.parse(res.data.data)
                    if (objData.code == 200) {
                        resolved(objData)
                    } else {
                        resolved(objData)
                    }
                } else {
                    rejected(res)
                }
            }).catch((error) => {
                rejected(error)
            })
        })
    },
    // 获取网关数据
    getData: ({ appid, interface_id, params = {}, columnId = '' }, file) => {
        const baseParams = new FormData()
        baseParams.append('version', '1.0')
        baseParams.append('charset', 'UTF-8')
        baseParams.append('origin', '1')
        baseParams.append('timestamp', new Date().getTime())
        baseParams.append('app_id', appid)
        baseParams.append('interface_id', interface_id)
        if (file) {
            baseParams.append('file', file)
        }
        const bizContent = columnId
            ? JSON.stringify(Object.assign({ columnId: columnId }, params))
            : JSON.stringify(params)
        baseParams.append('biz_content', bizContent)

        return new Promise((resolved, rejected) => {
            // 获取签名
            jpaasRequest.createSign(baseParams).then(async sign => {
                // 签名
                baseParams.append('sign', sign)
                // 网关请求
                try {
                    const response = await jpaasRequest.gateway(baseParams)
                    try {
                        let obj = JSON.parse(response)
                        if (typeof obj === 'object' && obj) {
                            resolved(obj)
                        } else {
                            resolved(obj)
                        }
                    } catch (error) {
                        resolved(response)
                    }
                } catch (error_1) {
                    rejected(error_1)
                }
            })
        })
    }
}
export default jpaasRequest